package com.zking.web.TL;

import com.zking.dao.domain.SysUser;
import org.apache.jasper.tagplugins.jstl.core.Redirect;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Tanglei
 * @date 2020/8/19 - 15:15
 */
@Controller
@RequestMapping(value = "/adminLogin",name = "后台")
public class BackLoginController {

    //去登陆
    @RequestMapping(value = "/toLogin", name = "去登陆")
    public String toLogin(){
        return "backend_page/login";
    }

    @RequestMapping(value = "/login",name = "登陆")
    public String submitLogin(String username, String password, HttpServletRequest request){
        System.out.println("==============登陆==============");
        try {
            //获取当前的用户对象
            Subject currentUser = SecurityUtils.getSubject();
            //判断当前用户是否被认证
            if (!currentUser.isAuthenticated()) {
                //令牌
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
                //记住密码
                token.setRememberMe(true);
                //执行登陆操作
                currentUser.login(token);
                //没有操作的情况下，多少毫秒之后T出登陆
                //shiro会默认拿这个数除以1000.默认去掉小数。
                //不能设置为0
                //永久有效，设置为-1000以上都行
                //设置再登陆之后
                //默认是30分钟1800000毫秒
                currentUser.getSession().setTimeout(1000000);//毫秒为单位
                SysUser user = (SysUser) currentUser.getPrincipal();
            }
        }catch (AuthenticationException e) {//认证异常
            request.setAttribute("msg", "用户名或密码错误");
            System.out.println("用户名或密码错误");
            return "backend_page/login";
        }
        return "backend_page/index";
    }


    //去首页
    @RequestMapping(value = "/adminIndex", name = "去后台首页")
    public String toAdminIndex(){
        return "backend_page/index";
    }

}
